package com.jack.leetcode.eachproblem_2020;

/**
 * Created by zhang_j on 2020/11/13
 */
public class 二叉树的直径 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
    static int left = 0, right = 0;
    public static int diameterOfBinaryTree(TreeNode root) {
        if(root == null){
            return 0;
        }
        recvTree(root.left, 1, true);
        recvTree(root.right, 1, false);
        System.out.println(left);
        System.out.println(right);
        return left + right - 2 >= 0 ? left + right - 2 : 0;
    }
    private static void recvTree(TreeNode root, int count, boolean isLeft){
        if(root == null){
            if(isLeft){
                left = Math.max(count, left);
            }else{
                right = Math.max(count, right);
            }
            return ;
        }
        recvTree(root.left, count + 1, isLeft);
        recvTree(root.right, count + 1, isLeft);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.right.right = new TreeNode(6);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        System.out.println(diameterOfBinaryTree(root));
    }
}
